import api from '@/api/index.js'
import {COMMENT_CREATE_RESULT, COMMENT_GET_RESULT,COMMENT_DELETE_RESULT} from "@/store/constant.js"


export const getCommentAction = (slug) => {
    return async (dispatch) => {
        try {
            const response = await api.comment.getComments(slug)
            if (response && response.status !== 1) {
                throw new Error(response.message)
            }
            const data = response.data;
            dispatch({type: COMMENT_GET_RESULT, payload: data});
        } catch (error) {
            throw new Error(error.message);
        }
    }

}

export const createCommentAction = (slug, body) => {
    return async (dispatch) => {
        try {
            const response = await api.comment.createComment(body, slug)
            if (response && response.status !== 1) {
                throw new Error(response.message)
            }
            const data = response.data;
            dispatch({type: COMMENT_CREATE_RESULT, payload: data});
        } catch (error) {
            throw new Error(error.message);
        }
    }
}


export const deleteCommentAction = (slug, commentId) => {
    return async (dispatch) => {
        try {
            const response = await api.comment.deleteComment(slug, commentId)
            if (response && response.status !== 1) {
                throw new Error(response.message)
            }
            dispatch({type: COMMENT_DELETE_RESULT, payload: commentId});
        } catch (error) {
            throw new Error(error.message);
        }
    }
}